package com.ojoin.trade.common.web.task;

import java.util.List;

import javax.servlet.ServletContext;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.web.domain.TbUploadFile;
import com.ojoin.trade.common.web.quartz.CronExpression;
import com.ojoin.trade.common.web.quartz.TaskGroup;
import com.ojoin.trade.common.web.service.TbUploadFileService;

@Component
public class FileCleanTask  extends BaseQuartzJob {
	
	private static final Logger logger = LoggerFactory
			.getLogger(FileCleanTask.class);
	
	
	@Autowired(required=false)
	private ServletContext servletContext;
	
	@Autowired
    private TbUploadFileService uploadFileService;
	
	@CronExpression(cron="0 0 5 ? * MON",desc="每周第一天5点清理一次 存活3天的文件")
    public void action() {
		
		List<TbUploadFile> files= uploadFileService.queryUnCommitAfter(3);
	   
		for(TbUploadFile file:files){
			if(StringUtils.isNotEmpty(file.getDownloadPath())){
				try{
					uploadFileService.deleteUnCommit(file);
				}catch(BusinessException e){
					LogUtils.info(logger, e.getMessage());
				}
			}
		}
    }
	

	@Override
	public TaskGroup getTaskGroupName() {
		return TaskGroup.DEFAULT;
	}
}